package com.tripleying.dogend.dnfipm;

import com.tripleying.dogend.dnfipm.controller.MainController;
import com.tripleying.dogend.dnfipm.controller.SelectController;
import com.tripleying.dogend.dnfipm.model.Version;
import com.tripleying.dogend.dnfipm.util.FileUtil;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;
import javafx.stage.StageStyle;

public class App extends Application {
    
    private static Stage stage;
    private static Scene scene;
    private static File folder;
    private static final Version version;
    
    static {
        version = new Version("1.5.0");
    }

    @Override
    public void start(Stage stage) throws IOException {
        App.stage = stage;
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setTitle("DNF补丁管理工具");
        String dnf = FileUtil.getConfig("dnf");
        if(dnf!=null){
            if(dnf.endsWith("ImagePacks2")){
                File file = new File(dnf);
                if(file.exists() && file.isDirectory()){
                    folder = file;
                    initMain();
                    return;
                }
            }
        }
        initSelect();
    }
    
    private void initSelect() throws IOException{
        scene = new Scene(FileUtil.loadFXML("select"));
        scene.setFill(null);
        stage.setScene(scene);
        stage.show();
    }
    
    private void initMain() throws IOException{
        scene = new Scene(FileUtil.loadFXML("main"));
        scene.setFill(null);
        stage.setScene(scene);
        stage.show();
        MainController.getInstance().init("DNF补丁管理工具-v"+version);
    }
    
    public static void switchSelect() throws IOException{
        scene.setRoot(FileUtil.loadFXML("select"));
        stage.show();
        SelectController.getInstance().setInitFolder(folder.getAbsolutePath());
    }
    
    public static void switchMain() throws IOException{
        scene.setRoot(FileUtil.loadFXML("main"));
        stage.show();
        MainController.getInstance().init("DNF补丁管理工具-v"+version);
    }
    
    public static void setFolder(File file){
        folder = file;
    }

    public static void main(String[] args) {
        launch();
    }
    
    public static Stage getStage(){
        return stage;
    }
    
    public static File getFolder(){
        return folder;
    }
    
    public static Version getVersion() {
        return version;
    }

    public static String getVersionString() {
        return version.toString();
    }

}